﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XTYAPI.PLC
{
    /// <summary>
    /// 适用于y=f(x),f(x)=kx+b的应用
    /// </summary>
    public class Fxy
    {
        /*
         Y
         |            P1(最大X，最大Y)
         |
         |
         | P2(最小X，最小Y)     
         |
         |______________________X
         */
        /// <summary>
        /// 因变量Y最大值
        /// </summary>
        public double Y_Max { get; set; }
        /// <summary>
        /// 因变量Y最小值
        /// </summary>
        public double Y_Min { get; set; }
        /// <summary>
        /// 自变量最大值
        /// </summary>
        public double X_Max { get; set; }
        /// <summary>
        /// 自变量最小值
        /// </summary>
        public double X_Min { get; set; }
        /// <summary>
        /// (Y_Max - Y_Min) / (X_Max - X_Min);比例值
        /// </summary>
        public double Y_X_K { get; set; }
        /// <summary>
        /// Y_Min - Y_X_K*X_Min;截距
        /// </summary>
        public double Y_B { get; set; }
        /// <summary>
        /// 设定函数常量
        /// </summary>
        public void Set_F_x()
        {
            //Y工,X数,K=（Y2-Y1)/(X2-X1).又Y=KX+B,求得K后，代入一个坐标即可求得B
            Y_X_K = (Y_Max - Y_Min) / (X_Max - X_Min);
            Y_B = Y_Min - Y_X_K*X_Min;
        }
        public double X_To_Y(double x)
        {
            //已知脉冲数 计算位置 位置Y=脉冲(X)*脉冲当量(K)+B位置
            return Y_X_K * x + Y_B;
        }
        public double Y_To_X(double y)
        {
            //已知工程Y求数学X
            //Y知 = K知*X(未知) + B知,所以X = (Y知 - B知) / K
            return (y - Y_B) / Y_X_K ;
        }
    }
}
